import 'package:json_annotation/json_annotation.dart';

part 'monthly_bill_model.g.dart';

/// 月度账单模型
@JsonSerializable()
class MonthlyBillModel {
  /// 年月
  @JsonKey(name: 'yearMonth')
  final String yearMonth;

  /// 总支出金额
  @JsonKey(name: 'totalExpenseMoney')
  final int totalExpenseMoney;

  /// 总收入金额
  @JsonKey(name: 'totalIncomeMoney')
  final int totalIncomeMoney;

  /// 月份（从yearMonth中提取）
  @JsonKey(includeFromJson: false, includeToJson: false)
  String get month => yearMonth.split('-')[1];

  /// 天月
  @JsonKey(name: 'dayMonth')
  final String? dayMonth;

  /// 构造函数
  MonthlyBillModel({
    required this.yearMonth,
    required this.totalExpenseMoney,
    required this.totalIncomeMoney,
    this.dayMonth,
  });

  /// 从JSON创建对象
  factory MonthlyBillModel.fromJson(Map<String, dynamic> json) =>
      _$MonthlyBillModelFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$MonthlyBillModelToJson(this);

  /// 获取格式化后的总支出
  String get formattedExpense => '¥${totalExpenseMoney.toStringAsFixed(2)}';

  /// 获取格式化后的总收入
  String get formattedIncome => '¥${totalIncomeMoney.toStringAsFixed(2)}';

  /// 获取月份显示文本
  String get formattedMonth => '${yearMonth.substring(0, 4)}年${int.parse(month)}月';
}

/// 月度账单响应模型
@JsonSerializable()
class MonthlyBillResponse {
  /// 状态码
  final int code;

  /// 消息
  final String message;

  /// 月度账单数据列表
  final List<MonthlyBillModel> data;

  /// 构造函数
  MonthlyBillResponse({
    required this.code,
    required this.message,
    required this.data,
  });

  /// 从JSON创建对象
  factory MonthlyBillResponse.fromJson(Map<String, dynamic> json) =>
      _$MonthlyBillResponseFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$MonthlyBillResponseToJson(this);
}